package com.wpml.product.controller;

import com.wpml.common.util.ResultJson;
import com.wpml.product.entity.CategoryModel;
import com.wpml.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 产品分类
 * @author: wp
 * @date: 2019-09-02 10:43
 **/
@RestController
@Slf4j
@RequestMapping("/product/category")
@Api(tags = "产品分类")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("获取所有分类(树形结构)")
    @GetMapping("/getCategoryWithTree")
    public ResultJson getAllCategory() {

        List<CategoryModel> entities = categoryService.listWithTree();

        return ResultJson.ok(entities);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public ResultJson info(@PathVariable("catId") Long catId){
        CategoryModel category = categoryService.getById(catId);

        return ResultJson.ok(category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加分类")
    //@RequiresPermissions("product:category:save")
    public ResultJson save(@RequestBody CategoryModel category){
        categoryService.save(category);

        return ResultJson.ok();
    }

    @PostMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public ResultJson updateSort(@RequestBody CategoryModel[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return ResultJson.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:category:update")
    public ResultJson update(@RequestBody CategoryModel category){
        categoryService.updateCascade(category);
        return ResultJson.ok();
    }


    /**
     * 删除
     * @RequestBody:获取请求体，必须发送POST请求
     * SpringMVC自动将请求体的数据（json），转为对应的对象
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public ResultJson delete(@RequestBody Long[] catIds){
        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return ResultJson.ok();
    }
}
